/**
 * 后台管理检查登录接口
 */

//引入Q模块
var Q = require('q');

//引入公共函数
var util = require('../../util');

//login入口函数
var index = function(getData, postData, config, redis, db) {
	var deferred = Q.defer();
	if(postData.session) {
		redis.get(postData.session).then(function(data) {
			if(data) {
				data = JSON.parse(data);
				db.connectDB('common').then(function(commonDB) {
					return commonDB.select('adminUsers', {userName: data.openId});
				}, function() {
					deferred.reject();
				}).then(function(adminUser) {
					if(!adminUser[0]) {
						deferred.reject('-0150');
					}
					else {
						deferred.resolve(adminUser[0].nickName || '未知');
					}
				}, function() {
					deferred.reject('-0149');
				});
			}
			else {
				deferred.reject('-0148');
			}
		}, function() {
			deferred.reject('-0147');
		});
	}
	else {
		//请求检查登录的参数不足
		deferred.reject('-0146');
	}
	return deferred.promise;
}

module.exports = {
	index: index
};